<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/13
 * Time: 23:49
 */
error_reporting(-1);
date_default_timezone_set('PRC');
require_once "config.php";
require_once "MyPDO.class.php";
$paydb = MyPDO::getInstance($host, $user, $pass, 'otherdata', 'utf8');
$db    = MyPDO::getInstance($host, $user, $pass, $dbName, 'utf8');
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth("qqq123123.");

while (1) {

    $json = $redis->brPop("alpayorder", 20);
    if (count($json) != 0) {
        $orders = json_decode($json[1], 1);
        $order  = $orders['order'];
        $admin  = $orders['user'];

        $date              = date("Y-m-d H:i:s", time() - 16 * 60 * 60);
        $felee_alipay_data = $paydb->query("felee_alipay_data", "`alipay_order` = '$order' AND `entertime` >= '$date'", "Row");
        if (!$felee_alipay_data['aid']) {
            $redis->lPush("alpayorder", json_encode($orders));
            echo "暂未找到" . PHP_EOL;
            sleep(5);
        } else {
            $arr = $db->query("cooperation_billing", "`order` = '$order'", "Row");
            print_r($arr);
            if (!$arr['id']) {
                $adminInfo = $db->query("cooperation", "`user` = '$admin'", "Row");
                $pay_money = $felee_alipay_data['pay_money'];

                $update              = [];
                $update['note']      = '系统充值';
                $update['change']    = $felee_alipay_data['pay_money'];
                $update['order']     = $felee_alipay_data['alipay_order'];
                $update['user']      = $admin;
                $update['entertime'] = $felee_alipay_data['entertime'];
                $db->insert("cooperation_billing", $update);
                $update          = [];
                $update['money'] = $adminInfo['money'] + $pay_money;
                $db->update("cooperation", $update, "`user` = '$admin'");
            } else {
                echo "已使用" . PHP_EOL;
            }
        }


    } else {
        echo "暂无" . PHP_EOL;
    }
}